package com.zzs.library.controller;

import com.zzs.library.common.api.CommonPage;
import com.zzs.library.common.api.CommonResult;
import com.zzs.library.mbg.model.Book;
import com.zzs.library.service.BookService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Author ZechariahZheng
 * @Date 2020/1/6 16:20
 * @Version 1.0
 * @description
 */
@Api(tags = "PmsBrandController", description = "小说管理")
@Controller
@RequestMapping("/index")
public class BookController {
    @Autowired
    BookService bookService;

    private static final Logger LOGGER = LoggerFactory.getLogger(BookController.class);

    /**
     * 查询所有小说
     * @return
     */
    @ApiOperation("获取所有小说")
    @GetMapping("/listAll")
    @ResponseBody
    public CommonResult<List<Book>> getBookList() {
        return CommonResult.success(bookService.listAllBook());
    }

    /**
     * 分页查询小说
     * @param pageNum 起始页数
     * @param pageSize 页数大小
     * @return
     */
    @ApiOperation("分页查询小说列表")
    @GetMapping("/list")
    @ResponseBody
    public CommonResult<CommonPage<Book>> listBook(@RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                                             @RequestParam(value = "pageSize", defaultValue = "3") Integer pageSize) {
        List<Book> bookList = bookService.listBook(pageNum, pageSize);
        return CommonResult.success(CommonPage.restPage(bookList));
    }

    /**
     * 查询单本小说
     * @param id
     * @return
     */
    @ApiOperation("获取指定id的小说")
    @GetMapping("/{id}")
    @ResponseBody
    public CommonResult<Book> book(@PathVariable("id") Long id) {
        LOGGER.info("id is: {}", id);
        return CommonResult.success(bookService.getBook(id));
    }
}
